package com.wanxi.servlet;

import com.wanxi.model.UserModel;
import com.wanxi.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String address = req.getParameter("address");
//        3.封装页面数据
//        UserModel userModel=new UserModel();
//        userModel.setAddress(address);
//        userModel.setUsername(username);
//        UserModel userModel=new UserModel(username,password,sex)
        UserModel userModel = UserModel.builder()
                .address(address)
                .username(username)
                .email(email)
                .sex(sex)
                .build();
//        4.处理逻辑关系
        UserService userService = new UserService();
        int result = userService.add(userModel);
//        5.将值返回给前端
        req.setAttribute("result",result);
        req.getRequestDispatcher("/user.jsp").forward(req,resp);
    }
}
